﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PGI.EN.Reportes;

namespace PGI.AD.Reportes
{
    public class ReportesAd
    {
        public static List<RptVentasEn> Reporte_Ventas(string fechaInicial, string fechaFinal)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "RptMovimientos";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@TipoMovimiento", 2);
                command.Parameters.AddWithValue("@fechaInicial", fechaInicial);
                command.Parameters.AddWithValue("@fechaFinal", fechaFinal);

                reader = command.ExecuteReader();
                List<RptVentasEn> lstReporte = new List<RptVentasEn>();
                RptVentasEn ventaEn = null;

                while (reader.Read())
                {
                    ventaEn = new RptVentasEn();
                    ventaEn.Fecha = reader.IsDBNull(0) ? string.Empty : reader.GetValue(0).ToString();
                    ventaEn.Codigo = reader.IsDBNull(1) ? string.Empty : reader.GetValue(1).ToString();
                    ventaEn.Categoria = reader.IsDBNull(2) ? string.Empty : reader.GetValue(2).ToString();
                    ventaEn.Marca = reader.IsDBNull(3) ? string.Empty : reader.GetValue(3).ToString();
                    ventaEn.Descripcion = reader.IsDBNull(4) ? string.Empty : reader.GetValue(4).ToString();
                    ventaEn.Talla = reader.IsDBNull(5) ? string.Empty : reader.GetValue(5).ToString();
                    ventaEn.Color = reader.IsDBNull(6) ? string.Empty : reader.GetValue(6).ToString();
                    ventaEn.Material = reader.IsDBNull(7) ? string.Empty : reader.GetValue(7).ToString();
                    ventaEn.Temporada = reader.IsDBNull(8) ? string.Empty : reader.GetValue(8).ToString();
                    ventaEn.Cantidad = reader.IsDBNull(9) ? 0 : Convert.ToInt32(reader.GetValue(9));
                    ventaEn.Precio_Venta = reader.IsDBNull(10) ? 0 : Convert.ToDouble(reader.GetValue(10));
                    ventaEn.Total_Venta = reader.IsDBNull(11) ? 0 : Convert.ToDouble(reader.GetValue(11));

                    lstReporte.Add(ventaEn);
                }

                return lstReporte;
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }
    }
}
